﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PID.Demo.Controls
{
    public class Line : Control
    {
        public Line()
        {
            this.Size = new System.Drawing.Size(200, 23);
            this.Padding = new System.Windows.Forms.Padding(0, 0, 0, 0);
        }
        private Color _lineColor = Color.Red;
        [Browsable(true)]
        public Color LineColor
        {
            get { return _lineColor; }
            set { _lineColor = value; this.Invalidate(); }
        }
        private int _lineSize = 2;
        public int LineSize
        {
            get => this._lineSize;
            set
            {
                if (value <= 0) return;
                this._lineSize = value;
                this.Invalidate();
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            SizeF txtSize = SizeF.Empty;
            if (!string.IsNullOrEmpty(this.Text))
            {
                txtSize = this.Text.MeasureString(this.Font);
                using (SolidBrush sb = new SolidBrush(this.ForeColor))
                {
                    e.Graphics.DrawString(Text, this.Font, sb, new PointF(this.Padding.Left, (this.Size.Height - this.Padding.Top - txtSize.Height) / 2f));
                }
            }
            using (SolidBrush sb = new SolidBrush(this.LineColor))
            {
                float x = txtSize == SizeF.Empty ? 0 : txtSize.Width;
                x += this.Padding.Left + 5;
                //float y = txtSize == SizeF.Empty ? (this.Size.Height - this.Padding.Top - LineSize) / 2f 
                //    : (this.Size.Height - this.Padding.Top - txtSize.Height) / 2f;
                float y = (this.Size.Height - this.Padding.Top - LineSize) / 2f;
                e.Graphics.FillRectangle(sb, new Rectangle((int)x, (int)y, (int)(this.Size.Width - x - this.Padding.Right), _lineSize));
            }
        }
    }
}
